{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d510c0e",
   "metadata": {},
   "source": [
    "# 基于百度智能云api的人工智能streamlit应用实现\n",
    "\n",
    "\n",
    "https://cloud.baidu.com/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef23404",
   "metadata": {},
   "source": [
    "## 第一个基于api接口的应用实现——文字识别（标准版）\n",
    "\n",
    "### 通过创建应用获得秘钥并写入如下代码实现图片中的文字识别\n",
    "\n",
    "百度智能云官网\n",
    "https://cloud.baidu.com/\n",
    "\n",
    "* 通过“开发者”-“示例代码中心”进入调用代码案例\n",
    "\n",
    "* 通过左上角菜单进入人工智能应用中心——选择需要的服务类型比如“文字识别”——领取免费接口——创建应用\n",
    "\n",
    "* 需要获取token\n",
    "\n",
    "* 所有内容，比如图片，音频都需要base64编码\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8cdbb93b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"words_result\":[{\"words\":\"1.1.1\"},{\"words\":\"数据导入\"},{\"words\":\"1.1.\"},{\"words\":\"数据分析与处理\"},{\"words\":\"、\"},{\"words\":\"1.1.3\"},{\"words\":\"模型的拟合\"},{\"words\":\"1.1.4模型的验证\"},{\"words\":\"1.1.5模型的使用\"},{\"words\":\"1.2机器学习模型的保存与载入\"},{\"words\":\"71.3\"},{\"words\":\"精准营销应用搭建\"},{\"words\":\"1.3.1数据导入\"},{\"words\":\"1.32数据分析与处理\"},{\"words\":\"1.3.3 classwork 2\"},{\"words\":\"1.3.4模型的拟合与验证\"},{\"words\":\"1.3.5机器学习的模型保存与读入\"},{\"words\":\"1.3.6模型的使用\"},{\"words\":\"1.3.7在streamlit中实现上面代码并实现模型参\"},{\"words\":\"1.3.8构建分页应用\"},{\"words\":\"1.3.9实现模型与数据的本地导入与保存\"},{\"words\":\"1.4用户流失预警系统的搭建\"}],\"words_result_num\":22,\"log_id\":1663716836657569053}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "API_KEY = \"\"\n",
    "SECRET_KEY = \"\"\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=\" + get_access_token()\n",
    "    \n",
    "    f=open(\"F:\\\\roc0.png\",\"rb\")\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    payload={\"image\":img}\n",
    "    headers = {\n",
    "        'Content-Type': 'application/x-www-form-urlencoded',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "\n",
    "def get_access_token():\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0a322553",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"words_result\":[{\"words\":\"1.1.1\"},{\"words\":\"数据导入\"},{\"words\":\"1.1.\"},{\"words\":\"数据分析与处理\"},{\"words\":\"、\"},{\"words\":\"1.1.3\"},{\"words\":\"模型的拟合\"},{\"words\":\"1.1.4模型的验证\"},{\"words\":\"1.1.5模型的使用\"},{\"words\":\"1.2机器学习模型的保存与载入\"},{\"words\":\"71.3\"},{\"words\":\"精准营销应用搭建\"},{\"words\":\"1.3.1数据导入\"},{\"words\":\"1.32数据分析与处理\"},{\"words\":\"1.3.3 classwork 2\"},{\"words\":\"1.3.4模型的拟合与验证\"},{\"words\":\"1.3.5机器学习的模型保存与读入\"},{\"words\":\"1.3.6模型的使用\"},{\"words\":\"1.3.7在streamlit中实现上面代码并实现模型参\"},{\"words\":\"1.3.8构建分页应用\"},{\"words\":\"1.3.9实现模型与数据的本地导入与保存\"},{\"words\":\"1.4用户流失预警系统的搭建\"}],\"words_result_num\":22,\"log_id\":1663166129779130070}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "import json\n",
    "\n",
    "f = open('F:\\\\roc0.png', 'rb')\n",
    "img = base64.b64encode(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49eed8a8",
   "metadata": {},
   "source": [
    "### 在streamlit 上实现文字识别应用\n",
    "\n",
    "* 通过st.file_uploader选择文件，展示对应的图片\n",
    "\n",
    "uploaded_file.getvalue()\n",
    "\n",
    "* 输出对应结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "322623dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1.1\n",
      "数据导入\n",
      "1.1.\n",
      "数据分析与处理\n",
      "、\n",
      "1.1.3\n",
      "模型的拟合\n",
      "1.1.4模型的验证\n",
      "1.1.5模型的使用\n",
      "1.2机器学习模型的保存与载入\n",
      "71.3\n",
      "精准营销应用搭建\n",
      "1.3.1数据导入\n",
      "1.32数据分析与处理\n",
      "1.3.3 classwork 2\n",
      "1.3.4模型的拟合与验证\n",
      "1.3.5机器学习的模型保存与读入\n",
      "1.3.6模型的使用\n",
      "1.3.7在streamlit中实现上面代码并实现模型参\n",
      "1.3.8构建分页应用\n",
      "1.3.9实现模型与数据的本地导入与保存\n",
      "1.4用户流失预警系统的搭建\n"
     ]
    }
   ],
   "source": [
    "for i in response1.json()['words_result']:\n",
    "    print(i[\"words\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9686dc2",
   "metadata": {},
   "source": [
    "## 在streamlit上实现人脸识别应用\n",
    "\n",
    "* 基于st.camera_input调取摄像头拍照"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "547d382f",
   "metadata": {},
   "outputs": [],
   "source": [
    "img_file_buffer = st.camera_input(\"Take a picture\")\n",
    "    \n",
    "if img_file_buffer is not None:\n",
    "    bytes_data = img_file_buffer.getvalue()\n",
    "    st.image(img_file_buffer)\n",
    "    img = base64.b64encode(bytes_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e53909a",
   "metadata": {},
   "source": [
    "## 在streamlit上实现图片二次元化\n",
    "\n",
    "* 注意输出图像需要解码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "720feb07",
   "metadata": {},
   "outputs": [],
   "source": [
    "st.image(base64.b64decode(response.json()[\"image\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6ee0c2b",
   "metadata": {},
   "source": [
    "## 关于语音识别的问题\n",
    "\n",
    "* 识别对于语音文件的采样频率和格式有比较严格的要求，导致容易出错，因此需要对语音文件进行改变，需要具备一定的语音信号知识\n",
    "\n",
    "b站上两个成功案例可以参考，但是注意要做网站app不能使用pyaudio，因为此包只能识别本机设备端口，无法通过网页识别客户端机器端口\n",
    "\n",
    "https://www.bilibili.com/video/BV1n4411c7vp/?spm_id_from=333.337.search-card.all.click&vd_source=09af776e3fe3bbe20ff8249e4a9b1581\n",
    "\n",
    "https://www.bilibili.com/video/BV1cF411e7mg/?spm_id_from=333.788.recommend_more_video.2&vd_source=09af776e3fe3bbe20ff8249e4a9b1581\n",
    "\n",
    "streamlit上好用的图像语音处理components，这些都可以实现客户端机器设备识别：\n",
    "\n",
    "https://github.com/stefanrmmr/streamlit_audio_recorder\n",
    "\n",
    "https://github.com/whitphx/streamlit-webrtc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "593d709f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
